package io.dshubs.archives.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.dshubs.archives.dao.ArchiveBaseRuleMapper;
import io.dshubs.archives.dto.ArchiveNumberRuleDto;
import io.dshubs.archives.entity.ArchiveBaseInfo;
import io.dshubs.archives.entity.ArchiveBaseRule;
import io.dshubs.archives.service.ArchiveBaseInfoService;
import io.dshubs.archives.service.ArchiveBaseRuleService;
import io.dshubs.commons.log.annotation.LogOperation;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.commons.tools.validator.ValidatorUtils;
import io.dshubs.commons.tools.validator.group.AddGroup;
import io.dshubs.commons.tools.validator.group.DefaultGroup;
import io.dshubs.commons.tools.validator.group.UpdateGroup;
import io.dshubs.dto.SysDictDataDTO;
import io.dshubs.dto.SysDictTypeDTO;
import io.dshubs.feign.DictFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档号规则 前端控制器
 * </p>
 *
 * @author tan
 * @since 2021-08-16
 */
@RestController
@Api(tags="全宗档号规则管理")
@RequestMapping("/rule")
public class ArchiveBaseRuleController {
    @Autowired
    private ArchiveBaseRuleService archiveBaseRuleService;

    @Autowired
    private ArchiveBaseRuleMapper archiveBaseRuleMapper;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Autowired
    private ArchiveBaseInfoService archiveBaseInfoService;

    @GetMapping("page")
    @ApiOperation("全宗规则:page")
    @LogOperation("全宗规则:page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = "ruleName", value = "规则名称", paramType = "query", dataType="String")
    })
    public Result<PageData<ArchiveBaseRule>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<PageData<ArchiveBaseRule>>().ok(archiveBaseRuleService.page(params));
    }


    @GetMapping("list")
    @ApiOperation("获取规则下拉")
    @LogOperation("全宗规则:list")
    public Result<List<ArchiveBaseRule>> list(){
        return new Result<List<ArchiveBaseRule>>().ok(archiveBaseRuleService.list());
    }



    @PostMapping
    @ApiOperation("保存")
    @LogOperation("全宗规则:save")
    public Result save(@RequestBody ArchiveNumberRuleDto archiveNumberRuleDto){
        archiveBaseRuleService.save(archiveNumberRuleDto);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("全宗规则:update")
    public Result update(@RequestBody ArchiveNumberRuleDto archiveNumberRuleDto){
        //效验数据
        ValidatorUtils.validateEntity(archiveNumberRuleDto, UpdateGroup.class, DefaultGroup.class);
        archiveBaseRuleService.save(archiveNumberRuleDto);
        return new Result();
    }

    @DeleteMapping()
    @ApiOperation("删除")
    @LogOperation("全宗规则:delete")
    public Result delete(@RequestBody @NotNull(message = "批量删除id不能为空") List<Long> ids){
        archiveBaseRuleService.deleteByName(ids);
        return new Result();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据全宗查找规则")
    @LogOperation("全宗规则:getRule")
    public Result<List<ArchiveBaseRule>> getRule(@PathVariable Long id){
        List<ArchiveBaseRule> baseRules = archiveBaseRuleMapper.selectList(new QueryWrapper<ArchiveBaseRule>().eq("archive_id", id));
        return new Result<List<ArchiveBaseRule> >().ok(baseRules);
    }

    @GetMapping("/field")
    @ApiOperation("根据全宗查找下拉基础字段")
    @LogOperation("全宗规则:getField")
    public Result<List<SysDictDataDTO>> getField(@NotNull(message = "id不能为空") Long id){
        ArchiveBaseInfo archiveBaseInfo = archiveBaseInfoService.selectById(id);
        Result<List<SysDictDataDTO>> listByCode = dictFeignClient.listByCode(archiveBaseInfo.getTenantCode());
        return new Result<List<SysDictDataDTO> >().ok(listByCode.getData());
    }
}
